package com.Logistics.pddispatch.service.impl;

import com.Logistics.BaseFeign.Driver_jobFeignClient;
import com.Logistics.truck.TrainNumberFeignClient;
import com.Logistics.truck.Transport_trips_truck_driverFeign;
import com.Logistics.truck.TruckFeignClient;
import com.Logistics.pddispatch.dto.OrderClassifyGroupDTO;
import com.Logistics.pddispatch.dto.OrderLineSimpleDTO;
import com.Logistics.pddispatch.dto.OrderLineTripsTruckDriverDTO;
import com.Logistics.dto.TrainNumber.TrainNumberByLineIdAndIdDto;
import com.Logistics.dto.truck.TransportTripsTruckDriverDto;
import com.Logistics.pddispatch.entity.CacheLineDetail;
import com.Logistics.entity.DriverJob;
import com.Logistics.pddispatch.entity.OrderClassIfyAttach;
import com.Logistics.entity.trainNumber.TrainNumberBean;
import com.Logistics.entity.truck.Transport_trips_truck_driverBean;
import com.Logistics.entity.truck.TruckBean;
import com.Logistics.pddispatch.service.OrderClassIfyAttachService;
import com.Logistics.pddispatch.service.TaskTripsSchedulingService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
public class TaskTripsSchedulingServiceImpl implements TaskTripsSchedulingService {
    @Autowired
    private TrainNumberFeignClient trainNumberFeignClient;
    @Autowired
    private Transport_trips_truck_driverFeign transportTripsTruckDriverFeign;
    @Autowired
    private TruckFeignClient truckFeignClient;
    @Autowired
    private Driver_jobFeignClient driverJobFeignClient;
    @Autowired
    private OrderClassIfyAttachService orderClassIfyAttachService;
    @Override
    public List<OrderLineTripsTruckDriverDTO> execute(List<OrderLineSimpleDTO> orderLineSimpleDTOS, String businessId, String jobId, String logId, String agencyId) {
        List<OrderLineTripsTruckDriverDTO> orderLineTripsTruckDriverDTOS = new ArrayList<>();

       orderLineSimpleDTOS.forEach(orderLineSimpleDTO -> {
           CacheLineDetail cacheLineDetail = orderLineSimpleDTO.getCacheLineDetailEntity();

           TrainNumberByLineIdAndIdDto dto = new TrainNumberByLineIdAndIdDto();
           dto.setTransportLineId(cacheLineDetail.getTransport_line_id());

           //根据线路查询车次信息
           List<TrainNumberBean> beanList = trainNumberFeignClient.getTrainNumberByLineIdAndId(dto);
           if (CollectionUtils.isEmpty(beanList)) {
               return;
           }

           LocalTime currentTime = LocalTime.now();
           //获取最近发车的车次
            TrainNumberBean trainNumber = beanList.stream()
                   .filter(obj -> LocalTime.parse(obj.getDeparture_time()).isAfter(currentTime))
                   .min((obj1, obj2) -> LocalTime.parse(obj1.getDeparture_time()).compareTo(LocalTime.parse(obj2.getDeparture_time())))
                   .orElse(null);
           //查询车辆和司机信息
               TransportTripsTruckDriverDto tripsTruckDriverDto = new TransportTripsTruckDriverDto();
               tripsTruckDriverDto.setTransportTripsId(trainNumber.getId());
               List<Transport_trips_truck_driverBean> transportTripsTruckDriverBeanList = transportTripsTruckDriverFeign.getTransportTripsTruckDriverById(tripsTruckDriverDto);
               Iterator<Transport_trips_truck_driverBean> iterator = transportTripsTruckDriverBeanList.iterator();
               while (iterator.hasNext()){
                   Transport_trips_truck_driverBean transportTripsTruckDriverBean = iterator.next();
                   TruckBean truckBean = this.truckFeignClient.getTruckById(transportTripsTruckDriverBean.getTruck_id());
                   DriverJob driverJob = this.driverJobFeignClient.getDriverJobById(transportTripsTruckDriverBean.getUser_id());
                   if (!ObjectUtils.isEmpty(truckBean) || ObjectUtils.isEmpty(driverJob)){
                       iterator.remove();
                   }else {
                       relation(transportTripsTruckDriverBean,orderLineSimpleDTO.getOrderClassifyGroupDTOS());
                   }
               }
               orderLineTripsTruckDriverDTOS.add(new OrderLineTripsTruckDriverDTO(transportTripsTruckDriverBeanList,
                       orderLineSimpleDTO.getCacheLineDetailEntity().getTransport_line_id(),orderLineSimpleDTO.getOrderClassifyGroupDTOS()));
           });
        return orderLineTripsTruckDriverDTOS;
    }

    /**
     * 绑定车次车辆司机订单分类
     * @param transportTripsTruckDriverBean
     * @param orderClassifyGroupDTOS
     */
    private void relation(Transport_trips_truck_driverBean transportTripsTruckDriverBean, List<OrderClassifyGroupDTO> orderClassifyGroupDTOS) {
        orderClassifyGroupDTOS.forEach(orderClassifyGroupDTO -> {
            OrderClassIfyAttach orderClassIfyAttach = new OrderClassIfyAttach();
            orderClassIfyAttach.setOrder_classify_id(orderClassifyGroupDTO.getId());
            orderClassIfyAttach.setTrips_id(transportTripsTruckDriverBean.getId());
            orderClassIfyAttach.setTruck_id(transportTripsTruckDriverBean.getTruck_id());
            orderClassIfyAttach.setDriver_id(transportTripsTruckDriverBean.getUser_id());
            orderClassIfyAttach.setCreate_date(new Date());
            this.orderClassIfyAttachService.save(orderClassIfyAttach);
        });
    }
}
